Difference between [NSMutableArray array] vs [[NSMutableArray alloc] init]?

That is exactly correct when you "vend" an object. But in other cases, when you create an object on iOS, where you have a choice between obtaining a ready-made autoreleased object and calling alloc followed by release, Apple wants you to use alloc and release, because this keeps the lifetime of the object short and under your control.

Because in the beginning I was declaring all my arrays with alloc and if in the end of a certain function I returned the array created with alloc I had to autorelease that array, because memory leak problems. Now using the first declaration I don't need to release anything That is exactly correct when you "vend" an object. But in other cases, when you create an object on iOS, where you have a choice between obtaining a ready-made autoreleased object and calling alloc followed by release, Apple wants you to use alloc and release, because this keeps the lifetime of the object short and under your control.

The problem here is that autoreleased objects live in the autorelease pool and can pile up until the pool is drained, whenever that may be. Another thing to watch out for is loops. You may generate autoreleased objects without being aware of it, and they just pile up in the pool.

The solution is to create your own autorelease pool at the start of the loop and release it at the end of the loop, so that the objects are released each time thru the loop. EDIT 12/18/2011: But with iOS 5 and the coming of ARC, the autorelease mechanism is far more efficient, and there is no such thing as release, so the distinction between alloc-init and a convenience constructor vending an autoreleased object becomes moot. (Also it's now an @autoreleasepool block rather than an autorelease pool (pseudo-)object.).

I should add that I've just written a book on iOS programming, and if you're just getting into this, the memory management chapter might be useful to you. It explains the details of what you're asking about here: apeth. Com/iOSBook/ch12.

Html – matt Mar 24 at 17:51 Great advice +1 – BoltClock? Mar 24 at 17:51 +1 :agree with BoltClock – Jhaliya Mar 24 at 17:57 The term "vend" is generally used in Cocoa to mean "make available through PDO. " What do you mean here?

– Chuck Mar 24 at 18:34.

The array class method by itself produces an autoreleased array, meaning you don't have to (and should not) release it manually.

– Adelino Mar 24 at 17:42 @Adelino: Yes as long as you don't send it a release message. – BoltClock? Mar 24 at 17:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions